﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace FtpImageQuery
{
    public class HttpClientUtil
    {
        /// <summary>
        /// 向目标地址提交图片文件参数数据
        /// </summary>
        /// <para name="url">url路径</para>
        /// <param name="fileBytes">文件字节流</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="waybillNo">运单号</param> 
        /// <param name="opCode">操作编码</param>
        /// <returns></returns>
        public static string HttpClientPostUpload(string url,byte[] fileBytes,string fileName,string clientNum="0", string waybillNo="",string opCode="310")
        {
            using (var client = new HttpClient())
            {
                List<ByteArrayContent> list = new List<ByteArrayContent>();

                var dataContent1 = new ByteArrayContent(Encoding.UTF8.GetBytes(waybillNo));
                dataContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "waybillNo"//Content-Type: image/jpeg
                };
                list.Add(dataContent1);

                var dataClientNum = new ByteArrayContent(Encoding.UTF8.GetBytes(clientNum));
                dataClientNum.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "clientOrder"//Content-Type: image/jpeg
                };
                list.Add(dataClientNum);

                var dataContent2 = new ByteArrayContent(Encoding.UTF8.GetBytes(opCode));
                dataContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "opCode"//Content-Type: image/jpeg
                };
                list.Add(dataContent2);

                var fileContent = new ByteArrayContent(fileBytes);//填充图片字节
                fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "file",
                    FileName = fileName,
                };
                list.Add(fileContent);

                using (var content = new MultipartFormDataContent())
                {
                    Action<List<ByteArrayContent>> act = (dataContents) =>
                    {//声明一个委托，该委托的作用就是将ByteArrayContent集合加入到MultipartFormDataContent中
                        foreach (var byteArrayContent in dataContents)
                        {
                            content.Add(byteArrayContent);
                        }
                    };

                    act(list);//执行act
                    try
                    {
                        var result = client.PostAsync(url, content).Result;//post请求
                        return result.Content.ReadAsStringAsync().Result;
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }

                }
            }
        }

        public static byte[] GetRometBytes(string url)
        {
            using (var client = new HttpClient())
            {
                var uri = new Uri(Uri.EscapeUriString(url));
                byte[] urlContents = client.GetByteArrayAsync(uri).Result;
                return urlContents;
            }
        }

        public static byte[] PostRometBytes(string url)
        {
            using (var client = new HttpClient())
            {
                var uri = new Uri(Uri.EscapeUriString(url));
                byte[] urlContents = client.GetByteArrayAsync(uri).Result;
                return urlContents;
            }
        }

        public static byte[] GetWebRequestByte(string url)
        {
            byte[] arraryByte;
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                StreamReader responseStream = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);
                int length = (int)wr.ContentLength;
                byte[] bs = new byte[length];

                HttpWebResponse response = wr as HttpWebResponse;
                Stream stream = response.GetResponseStream();

                //读取到内存
                MemoryStream stmMemory = new MemoryStream();
                byte[] buffer1 = new byte[length];
                int i;
                //将字节逐个放入到Byte 中
                while ((i = stream.Read(buffer1, 0, buffer1.Length)) > 0)
                {
                    stmMemory.Write(buffer1, 0, i);
                }
                arraryByte = stmMemory.ToArray();
                stmMemory.Close();
            }
            return arraryByte;
        }
    }
}